#include <QLabel>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QDebug>
#include <QtWidgets>
#include "clientlist.h"

ClientList::ClientList(QWidget *parent)
    : QListWidget(parent),
      s_EC(ExperimentConfiguration::get())
{

}
void ClientList::initPiece()
{
    //clientIpAddr = QString(tr("192.168.1.3"));
    //addPiece(clientIpAddr);
}

void ClientList::addPiece(ExperimentConfiguration::clientStatic addClient)
{
    QString ipAddr = addClient.clientIp;
    clientConfig.clientIp =  addClient.clientIp;
    clientConfig.tcpServer = addClient.tcpServer;
    clientConfig.clientInformation = addClient.clientInformation;
    QListWidgetItem *clientItem = new QListWidgetItem(this);
    QWidget *clientInfo = getClientInfo(ipAddr);
    clientInfo->setAttribute(Qt::WA_DeleteOnClose);
    qDebug()<<clientInfo->size()<<"client Info size";
    clientItem->setSizeHint(clientInfo->size());
    this->addItem(clientItem);
    this->setItemWidget(clientItem, clientInfo);
}
void ClientList::addSportItem()
{

}
QGroupBox *ClientList::getClientInfo(QString ipAddr)
{
    clientIpAddr = ipAddr;
    QLabel *clientNameLabel = new QLabel(tr("测试机"));
    clientNameLabel->setAlignment(Qt::AlignCenter);
    clientNameLabel->setFont(QFont("楷体", 16, QFont::Bold));

    QLabel *clientIpAddrLabel = new QLabel(clientIpAddr);
    clientStaticLabel = new QLabel(tr("未连接"));
    clientIpAddrLabel->setFont(QFont("楷体", 12, QFont::Bold));
    clientStaticLabel->setFont(QFont("楷体", 12, QFont::Bold));
    clientStopButton = new QPushButton(tr("停止"));
    clientStartButton = new QPushButton(tr("启动"));
    clientStopButton->setEnabled(false);
    clientStartButton->setEnabled(false);
    clientConnectionButton = new QPushButton(tr("连接"));

    connect(clientConnectionButton, &QPushButton::clicked, this, &ClientList::clientConnection);
    connect(clientStartButton, &QPushButton::clicked, this, &ClientList::clientStart);
    connect(clientStopButton, &QPushButton::clicked, this, &ClientList::clientStop);


    QDialogButtonBox *buttonBox = new QDialogButtonBox;
    buttonBox->addButton(clientConnectionButton, QDialogButtonBox::ApplyRole);
    buttonBox->addButton(clientStartButton, QDialogButtonBox::ApplyRole);
    buttonBox->addButton(clientStopButton, QDialogButtonBox::ApplyRole);

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(clientNameLabel,0,0);
    layout->addWidget(clientIpAddrLabel,1,0);
    layout->addWidget(clientStaticLabel,1,1);
    layout->addWidget(buttonBox,2,0);

    QGroupBox *layoutBox = new QGroupBox(this);
    layoutBox->setLayout(layout);
    layoutBox->setMaximumHeight(100);
    layoutBox->setMinimumHeight(100);

    return layoutBox;
}
void ClientList::clientConnection()
{
    if(clientConfig.tcpServer->getTcpServerStatic())
    {
        qDebug()<<"连接已经建立";
        return;
    }
    tcpSocketConnectStatic = clientConfig.tcpServer->Connection(clientIpAddr, 45454);
    if (tcpSocketConnectStatic)
    {
        clientConnectionButton->setEnabled(false);
        clientStaticLabel->setText(tr("已连接"));
        clientStopButton->setEnabled(true);
        clientStartButton->setEnabled(true);
        connect(clientConfig.tcpServer, SIGNAL(TcpSockerStatic(bool)), this, SLOT(updateTcpSocketView(bool)));
        return;
    }
}
void ClientList::updateTcpSocketView(bool flag)
{
    if(!flag)
    {
        clientStaticLabel->setText(tr("连接断开"));
        clientConnectionButton->setEnabled(true);
        clientStopButton->setEnabled(false);
        clientStartButton->setEnabled(false);
        return;
    }
    clientConnectionButton->setEnabled(false);
    clientStaticLabel->setText(tr("已连接"));
    clientStopButton->setEnabled(true);
    clientStartButton->setEnabled(true);
}
void ClientList::clientStart()
{
    // TODO 这里简单发送一个启动的命令
    qDebug()<<"点击发送";
    qint16 dataType = 1;
    qint16 truckStart = 1;
    clientConfig.clientInformation->STC_Data.DateType = dataType;   // 本包围数据包
    clientConfig.clientInformation->STC_Data.TruckStart = truckStart; // 前进挡启动
    QJsonObject sendJson = clientConfig.clientInformation->getSendToClientDataPackage();

    clientConfig.tcpServer->send(sendJson);

}
void ClientList::clientStop()
{
    clientConfig.clientInformation->STC_Data.DateType = 1;   // 本包围数据包
    clientConfig.clientInformation->STC_Data.TruckStart = 0; // 前进挡停止
    QJsonObject sendJson = clientConfig.clientInformation->getSendToClientDataPackage();
    clientConfig.tcpServer->send(sendJson);
}
